﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;

using Microsoft.IdentityModel.OAuth;

namespace AMT.IdentityModel.OAuth.Tests
{
	[TestClass]
	public class WrapHandlerValidity
	{
		private TestParameters m_params;

		[TestInitialize]
		public void Initialize()
		{
			m_params = TestParameters.LoadFromConfig();
		}

		#region WrapHandler constructor tests
		// NOTE: Rather than validating WrapHandler's input values in the constructor, validation occurs on call to RetrieveTokenFromSTS. This 
		//	provides for flexibility of construction & config before the call.  These tests, therefore, pass invalid values to the constructor,
		//	but don't expect an exception until attempting to retrieve the token.
		//	TODO: consider: validate in ctor & on call.
		[TestMethod]
		public void Ctor_NullSTSEndpoint()
		{
			AMT.IdentityModel.OAuth.WRAPHandler<Microsoft.IdentityModel.OAuth.SimpleWebToken, AMT.IdentityModel.OAuth.SWTUserPasswordClaimToken> wrapHandler =
						 new AMT.IdentityModel.OAuth.WRAPHandler<SimpleWebToken, AMT.IdentityModel.OAuth.SWTUserPasswordClaimToken>(
							 new Uri(m_params.STSBaseUri, m_params.WRAPEndpointPath), m_params.Scope);
			Assert.IsNotNull(wrapHandler);

			wrapHandler.ClaimToken = new SWTUserPasswordClaimToken(m_params.IdentityName, m_params.IdentityPassword);

			// Set the endpoint to null to trigger validation error
			wrapHandler.STSUri = null;
			try
			{
				wrapHandler.RetrieveTokenFromSTS();
			}
			catch (InvalidOperationException ex)
			{
				Assert.IsTrue(ex.Message.Equals("STSUri or Scope is invalid"));
			}
		}


		[TestMethod]
		public void Ctor_NullScope()
		{
			AMT.IdentityModel.OAuth.WRAPHandler<Microsoft.IdentityModel.OAuth.SimpleWebToken, AMT.IdentityModel.OAuth.SWTUserPasswordClaimToken> wrapHandler =
						 new AMT.IdentityModel.OAuth.WRAPHandler<SimpleWebToken, AMT.IdentityModel.OAuth.SWTUserPasswordClaimToken>(
							 new Uri(m_params.STSBaseUri, m_params.WRAPEndpointPath), m_params.Scope);
			Assert.IsNotNull(wrapHandler);

			wrapHandler.ClaimToken = new SWTUserPasswordClaimToken(m_params.IdentityName, m_params.IdentityPassword);

			// Set the endpoint to null to trigger validation error
			wrapHandler.Scope = null;
			try
			{
				wrapHandler.RetrieveTokenFromSTS();
			}
			catch (InvalidOperationException ex)
			{
				Assert.IsTrue(ex.Message.Equals("STSUri or Scope is invalid"));
			}
		}


		[TestMethod]
		public void Ctor_NullClaimToken()
		{
			AMT.IdentityModel.OAuth.WRAPHandler<Microsoft.IdentityModel.OAuth.SimpleWebToken, AMT.IdentityModel.OAuth.SWTUserPasswordClaimToken> wrapHandler =
						 new AMT.IdentityModel.OAuth.WRAPHandler<SimpleWebToken, AMT.IdentityModel.OAuth.SWTUserPasswordClaimToken>(
							 new Uri(m_params.STSBaseUri, m_params.WRAPEndpointPath), m_params.Scope);
			Assert.IsNotNull(wrapHandler);

			// Set the endpoint to null to trigger validation error
			wrapHandler.ClaimToken = null;

			try
			{
				wrapHandler.RetrieveTokenFromSTS();
			}
			catch (InvalidOperationException ex)
			{
				Assert.IsTrue(ex.Message.Equals("ClaimToken is invalid"));
			}
		}
		#endregion
	}
}
